package self.hoy.demo.grainmall.product.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.StringUtils;
import self.hoy.demo.common.utils.PageUtils;
import self.hoy.demo.common.utils.Query;

import self.hoy.demo.grainmall.product.entity.Attr;
import self.hoy.demo.grainmall.product.entity.AttrAttrgroupRelation;
import self.hoy.demo.grainmall.product.mapper.AttrGroupMapper;
import self.hoy.demo.grainmall.product.entity.AttrGroup;
import self.hoy.demo.grainmall.product.service.AttrAttrgroupRelationService;
import self.hoy.demo.grainmall.product.service.AttrGroupService;
import self.hoy.demo.grainmall.product.service.AttrService;
import self.hoy.demo.grainmall.product.vo.AttrGroupWithAttrsVo;
import self.hoy.demo.grainmall.product.vo.SpuItemAttrGroupVo;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroup> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationService relationService;

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroup> page = this.page(
                new Query<AttrGroup>().getPage(params),
                new QueryWrapper<AttrGroup>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        //select * from pms_attr_group where catelog_id = ?
        // and (attr_group_id = key or attr_group_name like %key%)
        QueryWrapper<AttrGroup> wrapper = new QueryWrapper<AttrGroup>();
        if (!StringUtils.isEmpty(key)) {
            wrapper.and((obj)->{
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        if (catelogId == null || catelogId.longValue() == 0L) {
            IPage<AttrGroup> page = this.page(
                    new Query<AttrGroup>().getPage(params), wrapper);
            return new PageUtils(page);
        } else {
            wrapper.eq("catelog_id", catelogId);
            IPage<AttrGroup> page = this.page(
                    new Query<AttrGroup>().getPage(params), wrapper);
            return new PageUtils(page);
        }
    }

    /**
     * 根据分类di查询所有分组及组内所有属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        // 查询指定分类下所有分组信息
        List<AttrGroup> groups = this.list(
                new QueryWrapper<AttrGroup>().eq("catelog_id", catelogId)
        );
        return groups == null || groups.isEmpty() ?
                null :
                groups.stream().map(g -> {
                    AttrGroupWithAttrsVo vo = new AttrGroupWithAttrsVo();
                    BeanUtils.copyProperties(g, vo);
                    List<Attr> attrsInGroup = attrService.getRelationAttr(g.getAttrGroupId());
                    vo.setAttrs(attrsInGroup);
                    return vo;
                }).collect(Collectors.toList());
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
//        当前spu对应了多少属性分组
        return baseMapper.getAttrGroup(spuId, catalogId);
    }

    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogssssssId(Long catelogId) {
        // 查询指定分类下所有分组信息
        List<AttrGroup> groups = this.list(
                new QueryWrapper<AttrGroup>().eq("catelog_id", catelogId)
        );
        if (groups == null || groups.isEmpty()) {
            return null;
        }
        List<Long> groupIds = groups.stream().map(g -> {
                    return g.getAttrGroupId();
                }).collect(Collectors.toList());

        List<AttrAttrgroupRelation> relations = groupIds == null || groupIds.isEmpty() ?
                null :
                relationService.list(
                        new QueryWrapper<AttrAttrgroupRelation>().in("attr_group_id", groupIds)
                );

        List<Long> attrIds = relations == null || relations.isEmpty() ?
                null :
                relations.stream().map(r -> {
                    return r.getAttrId();
                }).collect(Collectors.toList());

        List<Attr> attrs = (List<Attr>) attrService.listByIds(attrIds);
        return groups.stream().map(s -> {
            AttrGroupWithAttrsVo vo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(s, vo);
            List<Attr> attrsInGroup = attrs.stream().filter(a -> {
//                a.getG
                return true;
            }).collect(Collectors.toList());
            vo.setAttrs(attrsInGroup);
            return vo;
        }).collect(Collectors.toList());
    }

}